package com.example.visitedsys.Controller;

import com.example.visitedsys.Shrio.Bean.ResponseBean;
import com.example.visitedsys.annotation.ControllerEndpoint;
import com.example.visitedsys.informationGet.PageVO;
import com.example.visitedsys.informationGet.PermissionNodeVO;
import com.example.visitedsys.informationGet.RoleVO;
import com.example.visitedsys.service.PermissionService;
import com.example.visitedsys.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author  SongPeng
 * @date  2021/11/11 20:21
 * @version 1.0
 */

@Api(tags = "系统角色接口")
@RestController
@RequestMapping("/role")
public class RoleController {
    private static final Logger logger = LogManager.getLogger(RoleController.class);
    @ModelAttribute
    public void doString(){
        MDC.put("operateModuleName","角色管理模块");
    }

    @Autowired
    private RoleService roleService;


    @Autowired
    private PermissionService permissionService;


    /**
     * 角色授权
     *
     * @param mids
     * @return
     */
    @ApiOperation(value = "角色授权")
    @RequiresPermissions({"role:authority"})
    @PostMapping("/authority/{roleId}")
    public ResponseBean authority(@PathVariable("roleId") Long roleId, @RequestBody Long[] mids) {
        logger.info("角色授权");
        roleService.authority(roleId, mids);
        return ResponseBean.success();
    }

    /**
     * 角色拥有的菜单权限id和菜单树
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "角色菜单")
    @GetMapping("/findRoleMenu/{roleId}")
    public ResponseBean findRoleMenu(@PathVariable("roleId") Long roleId) {
        List<PermissionNodeVO> tree = permissionService.findMenuTree();
        //角色拥有的菜单id
        List<Long> mids = roleService.findMenuIdsByRoleId(roleId);
        List<Long> ids = permissionService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", tree);
        map.put("mids", mids);
        map.put("open", ids);
        return ResponseBean.success(map);
    }

    /**
     * 角色列表
     *
     * @return
     */
    @ApiOperation(value = "角色列表")
    @GetMapping("/findRoleList")
    public ResponseBean findRoleList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                     RoleVO roleVO) {
        PageVO<RoleVO> roleList = roleService.findRoleList(pageNum, pageSize, roleVO);
        return ResponseBean.success(roleList);
    }

    /**
     * 添加角色信息
     *
     * @param roleVO
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加角色失败", operation = "添加角色")
    @ApiOperation(value = "添加角色")
    @RequiresPermissions({"role:add"})
    @PostMapping("/add")
    public ResponseBean add(@RequestBody RoleVO roleVO) {
        logger.info("添加角色");
        roleService.add(roleVO);
        return ResponseBean.success();
    }

    /**
     * 删除角色
     *
     * @param roleId 角色ID
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除角色失败", operation = "删除角色")
    @ApiOperation(value = "删除角色", notes = "根据roleId删除角色信息")
    @RequiresPermissions({"role:delete"})
    @DeleteMapping("/delete/{roleId}")
    public ResponseBean delete(@PathVariable("roleId") Long roleId) {
        logger.info("删除角色");
        roleService.deleteById(roleId);
        return ResponseBean.success();
    }


    /**
     * 编辑角色信息
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "编辑用户", notes = "根据roleId更新角色信息")
    @GetMapping("/edit/{roleId}")
    @RequiresPermissions({"role:edit"})
    public ResponseBean edit(@PathVariable("roleId") Long roleId) {
        logger.info("编辑角色");
        RoleVO roleVO = roleService.edit(roleId);
        return ResponseBean.success(roleVO);
    }

    /**
     * 更新角色
     *
     * @param roleId
     * @param roleVO
     * @return
     */
    @ApiOperation(value = "更新角色", notes = "根据roleId更新角色信息")
    @RequiresPermissions({"role:update"})
    @PutMapping("/update/{roleId}")
    public ResponseBean update(@PathVariable("roleId") Long roleId, @RequestBody @Validated RoleVO roleVO) {
        logger.info("更新角色信息");
        roleService.update(roleId, roleVO);
        return ResponseBean.success();
    }

    /**
     * 更新角色状态
     *
     * @param roleId
     * @param status
     * @return
     */
    @ApiOperation(value = "更新状态", notes = "禁用和更新两种状态")
    @RequiresPermissions({"role:status"})
    @PutMapping("/updateStatus/{roleId}/{status}")
    public ResponseBean updateStatus(@PathVariable("roleId") Long roleId, @PathVariable Boolean status) {
        logger.info("更新角色状态");
        roleService.updateStatus(roleId, status);
        return ResponseBean.success();
    }

//    /**
//     * 导出excel
//     * @param response
//     */
//    @ApiOperation(value = "导出excel", notes = "导出所有角色的excel表格")
//    @PostMapping("/excel")
//    @RequiresPermissions("role:export")
//    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出角色excel")
//    public void export(HttpServletResponse response) {
//        List<Role> roles = this.roleService.findAll();
//        ExcelKit.$Export(Role.class, response).downXlsx(roles, false);
//    }


}
